home *** CD-ROM | disk | FTP | other *** search
/ ShareWare OnLine 2 / ShareWare OnLine Volume 2 (CMS Software)(1993).iso / os2 / elvis172.zip / regexp.c < prev    next >
C/C++ Source or Header  |  1993-01-06  |  19KB  |  972 lines

  1. /* regexp.c */
  2.  
  3. /* This file contains the code that compiles regular expressions and executes
  4.  * them.  It supports the same syntax and features as vi's regular expression
  5.  * code.  Specifically, the meta characters are:
  6.  *    ^    matches the beginning of a line
  7.  *    $    matches the end of a line
  8.  *    \<    matches the beginning of a word
  9.  *    \>    matches the end of a word
  10.  *    .    matches any single character
  11.  *    []    matches any character in a character class
  12.  *    \(    delimits the start of a subexpression
  13.  *    \)    delimits the end of a subexpression
  14.  *    *    repeats the preceding 0 or more times
  15.  * NOTE: You cannot follow a \) with a *.
  16.  *
  17.  * The physical structure of a compiled RE is as follows:
  18.  *    - First, there is a one-byte value that says how many character classes
  19.  *      are used in this regular expression
  20.  *    - Next, each character class is stored as a bitmap that is 256 bits
  21.  *      (32 bytes) long.
  22.  *    - A mixture of literal characters and compiled meta characters follows.
  23.  *      This begins with M_BEGIN(0) and ends with M_END(0).  All meta chars
  24.  *      are stored as a \n followed by a one-byte code, so they take up two
  25.  *      bytes apiece.  Literal characters take up one byte apiece.  \n can't
  26.  *      be used as a literal character.
  27.  *
  28.  * If NO_MAGIC is defined, then a different set of functions is used instead.
  29.  * That right, this file contains TWO versions of the code.
  30.  */
  31.  
  32. #include <setjmp.h>
  33. #include "config.h"
  34. #include "ctype.h"
  35. #include "vi.h"
  36. #include "regexp.h"
  37.  
  38.  
  39.  
  40. static char    *previous;    /* the previous regexp, used when null regexp is given */
  41.  
  42.  
  43. #ifndef NO_MAGIC
  44. /* THE REAL REGEXP PACKAGE IS USED UNLESS "NO_MAGIC" IS DEFINED */
  45.  
  46. /* These are used to classify or recognize meta-characters */
  47. #define META        '\0'
  48. #define BASE_META(m)    ((m) - 256)
  49. #define INT_META(c)    ((c) + 256)
  50. #define IS_META(m)    ((m) >= 256)
  51. #define IS_CLASS(m)    ((m) >= M_CLASS(0) && (m) <= M_CLASS(9))
  52. #define IS_START(m)    ((m) >= M_START(0) && (m) <= M_START(9))
  53. #define IS_END(m)    ((m) >= M_END(0) && (m) <= M_END(9))
  54. #define IS_CLOSURE(m)    ((m) >= M_SPLAT && (m) <= M_RANGE)
  55. #define ADD_META(s,m)    (*(s)++ = META, *(s)++ = BASE_META(m))
  56. #define GET_META(s)    (*(s) == META ? INT_META(*++(s)) : *s)
  57.  
  58. /* These are the internal codes used for each type of meta-character */
  59. #define M_BEGLINE    256        /* internal code for ^ */
  60. #define M_ENDLINE    257        /* internal code for $ */
  61. #define M_BEGWORD    258        /* internal code for \< */
  62. #define M_ENDWORD    259        /* internal code for \> */
  63. #define M_ANY        260        /* internal code for . */
  64. #define M_SPLAT        261        /* internal code for * */
  65. #define M_PLUS        262        /* internal code for \+ */
  66. #define M_QMARK        263        /* internal code for \? */
  67. #define M_RANGE        264        /* internal code for \{ */
  68. #define M_CLASS(n)    (265+(n))    /* internal code for [] */
  69. #define M_START(n)    (275+(n))    /* internal code for \( */
  70. #define M_END(n)    (285+(n))    /* internal code for \) */
  71.  
  72. /* These are used during compilation */
  73. static int    class_cnt;    /* used to assign class IDs */
  74. static int    start_cnt;    /* used to assign start IDs */
  75. static int    end_stk[NSUBEXP];/* used to assign end IDs */
  76. static int    end_sp;
  77. static char    *retext;    /* points to the text being compiled */
  78.  
  79. /* error-handling stuff */
  80. jmp_buf    errorhandler;
  81. #define FAIL(why)    regerror(why); longjmp(errorhandler, 1)
  82.  
  83.  
  84.  
  85.  
  86.  
  87. /* This function builds a bitmap for a particular class */
  88. static char *makeclass(text, bmap)
  89.     REG char    *text;    /* start of the class */
  90.     REG char    *bmap;    /* the bitmap */
  91. {
  92.     REG int        i;
  93.     int        complement = 0;
  94.  
  95.  
  96.     checkmem();
  97.  
  98.     /* zero the bitmap */
  99.     for (i = 0; bmap && i < 32; i++)
  100.     {
  101.         bmap[i] = 0;
  102.     }
  103.  
  104.     /* see if we're going to complement this class */
  105.     if (*text == '^')
  106.     {
  107.         text++;
  108.         complement = 1;
  109.     }
  110.  
  111.     /* add in the characters */
  112.     while (*text && *text != ']')
  113.     {
  114.         /* is this a span of characters? */
  115.         if (text[1] == '-' && text[2])
  116.         {
  117.             /* spans can't be backwards */
  118.             if (text[0] > text[2])
  119.             {
  120.                 FAIL("Backwards span in []");
  121.             }
  122.  
  123.             /* add each character in the span to the bitmap */
  124.             for (i = UCHAR(text[0]); bmap && (unsigned)i <= UCHAR(text[2]); i++)
  125.             {
  126.                 bmap[i >> 3] |= (1 << (i & 7));
  127.             }
  128.  
  129.             /* move past this span */
  130.             text += 3;
  131.         }
  132.         else
  133.         {
  134.             /* add this single character to the span */
  135.             i = *text++;
  136.             if (bmap)
  137.             {
  138.                 bmap[UCHAR(i) >> 3] |= (1 << (UCHAR(i) & 7));
  139.             }
  140.         }
  141.     }
  142.  
  143.     /* make sure the closing ] is missing */
  144.     if (*text++ != ']')
  145.     {
  146.         FAIL("] missing");
  147.     }
  148.  
  149.     /* if we're supposed to complement this class, then do so */
  150.     if (complement && bmap)
  151.     {
  152.         for (i = 0; i < 32; i++)
  153.         {
  154.             bmap[i] = ~bmap[i];
  155.         }
  156.     }
  157.  
  158.     checkmem();
  159.  
  160.     return text;
  161. }
  162.  
  163.  
  164.  
  165.  
  166. /* This function gets the next character or meta character from a string.
  167.  * The pointer is incremented by 1, or by 2 for \-quoted characters.  For [],
  168.  * a bitmap is generated via makeclass() (if re is given), and the
  169.  * character-class text is skipped.
  170.  */
  171. static int gettoken(sptr, re)
  172.     char    **sptr;
  173.     regexp    *re;
  174. {
  175.     int    c;
  176.  
  177.     c = **sptr;
  178.     if (!c)
  179.     {
  180.         return c;
  181.     }
  182.     ++*sptr;
  183.     if (c == '\\')
  184.     {
  185.         c = **sptr;
  186.         ++*sptr;
  187.         switch (c)
  188.         {
  189.           case '<':
  190.             return M_BEGWORD;
  191.  
  192.           case '>':
  193.             return M_ENDWORD;
  194.  
  195.           case '(':
  196.             if (start_cnt >= NSUBEXP)
  197.             {
  198.                 FAIL("Too many \\(s");
  199.             }
  200.             end_stk[end_sp++] = start_cnt;
  201.             return M_START(start_cnt++);
  202.  
  203.           case ')':
  204.             if (end_sp <= 0)
  205.             {
  206.                 FAIL("Mismatched \\)");
  207.             }
  208.             return M_END(end_stk[--end_sp]);
  209.  
  210.           case '*':
  211.             return (*o_magic ? c : M_SPLAT);
  212.  
  213.           case '.':
  214.             return (*o_magic ? c : M_ANY);
  215.  
  216.           case '+':
  217.             return M_PLUS;
  218.  
  219.           case '?':
  220.             return M_QMARK;
  221. #ifndef CRUNCH
  222.           case '{':
  223.             return M_RANGE;
  224. #endif
  225.           default:
  226.             return c;
  227.         }
  228.     }
  229.     else if (*o_magic)
  230.     {
  231.         switch (c)
  232.         {
  233.           case '^':
  234.             if (*sptr == retext + 1)
  235.             {
  236.                 return M_BEGLINE;
  237.             }
  238.             return c;
  239.  
  240.           case '$':
  241.             if (!**sptr)
  242.             {
  243.                 return M_ENDLINE;
  244.             }
  245.             return c;
  246.  
  247.           case '.':
  248.             return M_ANY;
  249.  
  250.           case '*':
  251.             return M_SPLAT;
  252.  
  253.           case '[':
  254.             /* make sure we don't have too many classes */
  255.             if (class_cnt >= 10)
  256.             {
  257.                 FAIL("Too many []s");
  258.             }
  259.  
  260.             /* process the character list for this class */
  261.             if (re)
  262.             {
  263.                 /* generate the bitmap for this class */
  264.                 *sptr = makeclass(*sptr, re->program + 1 + 32 * class_cnt);
  265.             }
  266.             else
  267.             {
  268.                 /* skip to end of the class */
  269.                 *sptr = makeclass(*sptr, (char *)0);
  270.             }
  271.             return M_CLASS(class_cnt++);
  272.  
  273.           default:
  274.             return c;
  275.         }
  276.     }
  277.     else    /* unquoted nomagic */
  278.     {
  279.         switch (c)
  280.         {
  281.           case '^':
  282.             if (*sptr == retext + 1)
  283.             {
  284.                 return M_BEGLINE;
  285.             }
  286.             return c;
  287.  
  288.           case '$':
  289.             if (!**sptr)
  290.             {
  291.                 return M_ENDLINE;
  292.             }
  293.             return c;
  294.  
  295.           default:
  296.             return c;
  297.         }
  298.     }
  299.     /*NOTREACHED*/
  300. }
  301.  
  302.  
  303.  
  304.  
  305. /* This function calculates the number of bytes that will be needed for a
  306.  * compiled RE.  Its argument is the uncompiled version.  It is not clever
  307.  * about catching syntax errors; that is done in a later pass.
  308.  */
  309. static unsigned calcsize(text)
  310.     char        *text;
  311. {
  312.     unsigned    size;
  313.     int        token;
  314.  
  315.     retext = text;
  316.     class_cnt = 0;
  317.     start_cnt = 1;
  318.     end_sp = 0;
  319.     size = 5;
  320.     while ((token = gettoken(&text, (regexp *)0)) != 0)
  321.     {
  322.         if (IS_CLASS(token))
  323.         {
  324.             size += 34;
  325.         }
  326. #ifndef CRUNCH
  327.         else if (token == M_RANGE)
  328.         {
  329.             size += 4;
  330.             while ((token = gettoken(&text, (regexp *)0)) != 0
  331.                 && token != '}')
  332.             {
  333.             }
  334.             if (!token)
  335.             {
  336.                 return size;
  337.             }
  338.         }
  339. #endif
  340.         else if (IS_META(token))
  341.         {
  342.             size += 2;
  343.         }
  344.         else
  345.         {
  346.             size++;
  347.         }
  348.     }
  349.  
  350.     return size;
  351. }
  352.  
  353.  
  354.  
  355. /* This function compiles a regexp. */
  356. regexp *regcomp(exp)
  357.     char        *exp;
  358. {
  359.     int        needfirst;
  360.     unsigned    size;
  361.     int        token;
  362.     int        peek;
  363.     char        *build;
  364. #if __STDC__
  365.     volatile
  366. #endif
  367.     regexp        *re;
  368. #ifndef CRUNCH
  369.     int        from;
  370.     int        to;
  371.     int        digit;
  372. #endif
  373. #ifdef DEBUG
  374.     int        calced;
  375. #endif
  376.  
  377.  
  378.     checkmem();
  379.  
  380.     /* prepare for error handling */
  381.     re = (regexp *)0;
  382.     if (setjmp(errorhandler))
  383.     {
  384.         checkmem();
  385.         if (re)
  386.         {
  387.             _free_(re);
  388.         }
  389.         return (regexp *)0;
  390.     }
  391.  
  392.     /* if an empty regexp string was given, use the previous one */
  393.     if (*exp == 0)
  394.     {
  395.         if (!previous)
  396.         {
  397.             FAIL("No previous RE");
  398.         }
  399.         exp = previous;
  400.     }
  401.     else /* non-empty regexp given, so remember it */
  402.     {
  403.         if (previous)
  404.             _free_(previous);
  405.         previous = (char *)malloc((unsigned)(strlen(exp) + 1));
  406.         if (previous)
  407.             strcpy(previous, exp);
  408.     }
  409.  
  410.     /* allocate memory */
  411.     checkmem();
  412.     class_cnt = 0;
  413.     start_cnt = 1;
  414.     end_sp = 0;
  415.     retext = exp;
  416. #ifdef DEBUG
  417.     calced = calcsize(exp);
  418.     size = calced + sizeof(regexp);
  419. #else
  420.     size = calcsize(exp) + sizeof(regexp) + 10; /* !!! 10 bytes for slop */
  421. #endif
  422. #ifdef lint
  423.     re = (regexp *)0;
  424. #else
  425.     re = (regexp *)malloc((unsigned)size);
  426. #endif
  427.     if (!re)
  428.     {
  429.         FAIL("Not enough memory for this RE");
  430.     }
  431.     checkmem();
  432.  
  433.     /* compile it */
  434.     build = &re->program[1 + 32 * class_cnt];
  435.     re->program[0] = class_cnt;
  436.     for (token = 0; token < NSUBEXP; token++)
  437.     {
  438.         re->startp[token] = re->endp[token] = (char *)0;
  439.     }
  440.     re->first = 0;
  441.     re->bol = 0;
  442.     re->minlen = 0;
  443.     needfirst = 1;
  444.     class_cnt = 0;
  445.     start_cnt = 1;
  446.     end_sp = 0;
  447.     retext = exp;
  448.     for (token = M_START(0), peek = gettoken(&exp, re);
  449.          token;
  450.          token = peek, peek = gettoken(&exp, re))
  451.     {
  452.         /* special processing for the closure operator */
  453.         if (IS_CLOSURE(peek))
  454.         {
  455.             /* detect misuse of closure operator */
  456.             if (IS_START(token))
  457.             {
  458.                 FAIL("Closure operator follows nothing");
  459.             }
  460.             else if (IS_META(token) && token != M_ANY && !IS_CLASS(token))
  461.             {
  462.                 FAIL("Closure operators can only follow a normal character or . or []");
  463.             }
  464.  
  465. #ifndef CRUNCH
  466.             /* if \{ \} then read the range */
  467.             if (peek == M_RANGE)
  468.             {
  469.                 from = 0;
  470.                 for (digit = gettoken(&exp, re);
  471.                      !IS_META(digit) && isdigit(digit);
  472.                      digit = gettoken(&exp, re))
  473.                 {
  474.                     from = from * 10 + digit - '0';
  475.                 }
  476.                 if (digit == '}')
  477.                 {
  478.                     to = from;
  479.                 }
  480.                 else if (digit == ',')
  481.                 {
  482.                     to = 0;
  483.                     for (digit = gettoken(&exp, re);
  484.                          !IS_META(digit) && isdigit(digit);
  485.                          digit = gettoken(&exp, re))
  486.                     {
  487.                         to = to * 10 + digit - '0';
  488.                     }
  489.                     if (to == 0)
  490.                     {
  491.                         to = 255;
  492.                     }
  493.                 }
  494.                 if (digit != '}')
  495.                 {
  496.                     FAIL("Bad characters after \\{");
  497.                 }
  498.                 else if (to < from || to == 0 || from >= 255)
  499.                 {
  500.                     FAIL("Invalid range for \\{ \\}");
  501.                 }
  502.                 re->minlen += from;
  503.             }
  504.             else
  505. #endif
  506.             if (peek != M_SPLAT)
  507.             {
  508.                 re->minlen++;
  509.             }
  510.  
  511.             /* it is okay -- make it prefix instead of postfix */
  512.             ADD_META(build, peek);
  513. #ifndef CRUNCH
  514.             if (peek == M_RANGE)
  515.             {
  516.                 *build++ = from;
  517.                 *build++ = (to < 255 ? to : 255);
  518.             }
  519. #endif
  520.             
  521.  
  522.             /* take care of "needfirst" - is this the first char? */
  523.             if (needfirst && peek == M_PLUS && !IS_META(token))
  524.             {
  525.                 re->first = token;
  526.             }
  527.             needfirst = 0;
  528.  
  529.             /* we used "peek" -- need to refill it */
  530.             peek = gettoken(&exp, re);
  531.             if (IS_CLOSURE(peek))
  532.             {
  533.                 FAIL("* or \\+ or \\? doubled up");
  534.             }
  535.         }
  536.         else if (!IS_META(token))
  537.         {
  538.             /* normal char is NOT argument of closure */
  539.             if (needfirst)
  540.             {
  541.                 re->first = token;
  542.                 needfirst = 0;
  543.             }
  544.             re->minlen++;
  545.         }
  546.         else if (token == M_ANY || IS_CLASS(token))
  547.         {
  548.             /* . or [] is NOT argument of closure */
  549.             needfirst = 0;
  550.             re->minlen++;
  551.         }
  552.  
  553.         /* the "token" character is not closure -- process it normally */
  554.         if (token == M_BEGLINE)
  555.         {
  556.             /* set the BOL flag instead of storing M_BEGLINE */
  557.             re->bol = 1;
  558.         }
  559.         else if (IS_META(token))
  560.         {
  561.             ADD_META(build, token);
  562.         }
  563.         else
  564.         {
  565.             *build++ = token;
  566.         }
  567.     }
  568.     checkmem();
  569.  
  570.     /* end it with a \) which MUST MATCH the opening \( */
  571.     ADD_META(build, M_END(0));
  572.     if (end_sp > 0)
  573.     {
  574.         FAIL("Not enough \\)s");
  575.     }
  576.  
  577. #ifdef DEBUG
  578.     if ((int)(build - re->program) != calced)
  579.     {
  580.         msg("regcomp error: calced=%d, actual=%d", calced, (int)(build - re->program));
  581.         getkey(0);
  582.     }
  583. #endif
  584.  
  585.     checkmem();
  586.     return re;
  587. }
  588.  
  589.  
  590.  
  591. /*---------------------------------------------------------------------------*/
  592.  
  593.  
  594. /* This function checks for a match between a character and a token which is
  595.  * known to represent a single character.  It returns 0 if they match, or
  596.  * 1 if they don't.
  597.  */
  598. int match1(re, ch, token)
  599.     regexp        *re;
  600.     REG char    ch;
  601.     REG int        token;
  602. {
  603.     if (!ch)
  604.     {
  605.         /* the end of a line can't match any RE of width 1 */
  606.         return 1;
  607.     }
  608.     if (token == M_ANY)
  609.     {
  610.         return 0;
  611.     }
  612.     else if (IS_CLASS(token))
  613.     {
  614.         if (re->program[1 + 32 * (token - M_CLASS(0)) + (UCHAR(ch) >> 3)] & (1 << (UCHAR(ch) & 7)))
  615.             return 0;
  616.     }
  617.     else if (ch == token || *o_ignorecase && tolower(ch) == tolower(token))
  618.     {
  619.         return 0;
  620.     }
  621.     return 1;
  622. }
  623.  
  624.  
  625.  
  626. /* This function checks characters up to and including the next closure, at
  627.  * which point it does a recursive call to check the rest of it.  This function
  628.  * returns 0 if everything matches, or 1 if something doesn't match.
  629.  */
  630. int match(re, str, prog, here)
  631.     regexp        *re;    /* the regular expression */
  632.     char        *str;    /* the string */
  633.     REG char    *prog;    /* a portion of re->program, an compiled RE */
  634.     REG char    *here;    /* a portion of str, the string to compare it to */
  635. {
  636.     REG int        token;    /* the roken pointed to by prog */
  637.     REG int        nmatched;/* counter, used during closure matching */ 
  638.     REG int        closure;/* the token denoting the type of closure */
  639.     int        from;    /* minimum number of matches in closure */
  640.     int        to;    /* maximum number of matches in closure */
  641.  
  642.     for (token = GET_META(prog); !IS_CLOSURE(token); prog++, token = GET_META(prog))
  643.     {
  644.         switch (token)
  645.         {
  646.         /*case M_BEGLINE: can't happen; re->bol is used instead */
  647.           case M_ENDLINE:
  648.             if (*here)
  649.                 return 1;
  650.             break;
  651.  
  652.           case M_BEGWORD:
  653.             if (here != str &&
  654.                (here[-1] == '_' || isalnum(here[-1])))
  655.                 return 1;
  656.             break;
  657.  
  658.           case M_ENDWORD:
  659.             if (here[0] == '_' || isalnum(here[0]))
  660.                 return 1;
  661.             break;
  662.  
  663.           case M_START(0):
  664.           case M_START(1):
  665.           case M_START(2):
  666.           case M_START(3):
  667.           case M_START(4):
  668.           case M_START(5):
  669.           case M_START(6):
  670.           case M_START(7):
  671.           case M_START(8):
  672.           case M_START(9):
  673.             re->startp[token - M_START(0)] = (char *)here;
  674.             break;
  675.  
  676.           case M_END(0):
  677.           case M_END(1):
  678.           case M_END(2):
  679.           case M_END(3):
  680.           case M_END(4):
  681.           case M_END(5):
  682.           case M_END(6):
  683.           case M_END(7):
  684.           case M_END(8):
  685.           case M_END(9):
  686.             re->endp[token - M_END(0)] = (char *)here;
  687.             if (token == M_END(0))
  688.             {
  689.                 return 0;
  690.             }
  691.             break;
  692.  
  693.           default: /* literal, M_CLASS(n), or M_ANY */
  694.             if (match1(re, *here, token) != 0)
  695.                 return 1;
  696.             here++;
  697.         }
  698.     }
  699.  
  700.     /* C L O S U R E */
  701.  
  702.     /* step 1: see what we have to match against, and move "prog" to point
  703.      * to the remainder of the compiled RE.
  704.      */
  705.     closure = token;
  706.     prog++;
  707.     switch (closure)
  708.     {
  709.       case M_SPLAT:
  710.         from = 0;
  711.         to = strlen(str);    /* infinity */
  712.         break;
  713.  
  714.       case M_PLUS:
  715.         from = 1;
  716.         to = strlen(str);    /* infinity */
  717.         break;
  718.  
  719.       case M_QMARK:
  720.         from = 0;
  721.         to = 1;
  722.         break;
  723.  
  724. #ifndef CRUNCH
  725.       case M_RANGE:
  726.         from = UCHAR(*prog++);
  727.         to = UCHAR(*prog++);
  728.         if (to == 255)
  729.         {
  730.             to = strlen(str); /* infinity */
  731.         }
  732.         break;
  733. #endif
  734.     }
  735.     token = GET_META(prog);
  736.     prog++;
  737.  
  738.     /* step 2: see how many times we can match that token against the string */
  739.     for (nmatched = 0;
  740.          nmatched < to && *here && match1(re, *here, token) == 0;
  741.          nmatched++, here++)
  742.     {
  743.     }
  744.  
  745.     /* step 3: try to match the remainder, and back off if it doesn't */
  746.     while (nmatched >= from && match(re, str, prog, here) != 0)
  747.     {
  748.         nmatched--;
  749.         here--;
  750.     }
  751.  
  752.     /* so how did it work out? */
  753.     if (nmatched >= from)
  754.         return 0;
  755.     return 1;
  756. }
  757.  
  758.  
  759.  
  760. /* This function searches through a string for text that matches an RE. */
  761. int regexec(re, str, bol)
  762.     regexp    *re;    /* the compiled regexp to search for */
  763.     char    *str;    /* the string to search through */
  764.     int    bol;    /* boolean: does str start at the beginning of a line? */
  765. {
  766.     char    *prog;    /* the entry point of re->program */
  767.     int    len;    /* length of the string */
  768.     REG char    *here;
  769.  
  770.     checkmem();
  771.  
  772.     /* if must start at the beginning of a line, and this isn't, then fail */
  773.     if (re->bol && !bol)
  774.     {
  775.         return 0;
  776.     }
  777.  
  778.     len = strlen(str);
  779.     prog = re->program + 1 + 32 * re->program[0];
  780.  
  781.     /* search for the RE in the string */
  782.     if (re->bol)
  783.     {
  784.         /* must occur at BOL */
  785.         if ((re->first
  786.             && match1(re, *(char *)str, re->first))/* wrong first letter? */
  787.          || len < re->minlen            /* not long enough? */
  788.          || match(re, (char *)str, prog, str))    /* doesn't match? */
  789.             return 0;            /* THEN FAIL! */
  790.     }
  791. #ifndef CRUNCH
  792.     else if (!*o_ignorecase)
  793.     {
  794.         /* can occur anywhere in the line, noignorecase */
  795.         for (here = (char *)str;
  796.              (re->first && re->first != *here)
  797.             || match(re, (char *)str, prog, here);
  798.              here++, len--)
  799.         {
  800.             if (len < re->minlen)
  801.                 return 0;
  802.         }
  803.     }
  804. #endif
  805.     else
  806.     {
  807.         /* can occur anywhere in the line, ignorecase */
  808.         for (here = (char *)str;
  809.              (re->first && match1(re, *here, (int)re->first))
  810.             || match(re, (char *)str, prog, here);
  811.              here++, len--)
  812.         {
  813.             if (len < re->minlen)
  814.                 return 0;
  815.         }
  816.     }
  817.  
  818.     /* if we didn't fail, then we must have succeeded */
  819.     checkmem();
  820.     return 1;
  821. }
  822.  
  823. /*============================================================================*/
  824. #else /* NO_MAGIC */
  825.  
  826. regexp *regcomp(exp)
  827.     char    *exp;
  828. {
  829.     char    *src;
  830.     char    *dest;
  831.     regexp    *re;
  832.     int    i;
  833.  
  834.     /* allocate a big enough regexp structure */
  835. #ifdef lint
  836.     re = (regexp *)0;
  837. #else
  838.     re = (regexp *)malloc((unsigned)(strlen(exp) + 1 + sizeof(struct regexp)));
  839. #endif
  840.     if (!re)
  841.     {
  842.         regerror("Could not malloc a regexp structure");
  843.         return (regexp *)0;
  844.     }
  845.  
  846.     /* initialize all fields of the structure */
  847.     for (i = 0; i < NSUBEXP; i++)
  848.     {
  849.         re->startp[i] = re->endp[i] = (char *)0;
  850.     }
  851.     re->minlen = 0;
  852.     re->first = 0;
  853.     re->bol = 0;
  854.  
  855.     /* copy the string into it, translating ^ and $ as needed */
  856.     for (src = exp, dest = re->program + 1; *src; src++)
  857.     {
  858.         switch (*src)
  859.         {
  860.           case '^':
  861.             if (src == exp)
  862.             {
  863.                 re->bol += 1;
  864.             }
  865.             else
  866.             {
  867.                 *dest++ = '^';
  868.                 re->minlen++;
  869.             }
  870.             break;
  871.  
  872.           case '$':
  873.             if (!src[1])
  874.             {
  875.                 re->bol += 2;
  876.             }
  877.             else
  878.             {
  879.                 *dest++ = '$';
  880.                 re->minlen++;
  881.             }
  882.             break;
  883.  
  884.           case '\\':
  885.             if (src[1])
  886.             {
  887.                 *dest++ = *++src;
  888.                 re->minlen++;
  889.             }
  890.             else
  891.             {
  892.                 regerror("extra \\ at end of regular expression");
  893.             }
  894.             break;
  895.  
  896.           default:
  897.             *dest++ = *src;
  898.             re->minlen++;
  899.         }
  900.     }
  901.     *dest = '\0';
  902.  
  903.     return re;
  904. }
  905.  
  906.  
  907. /* This "helper" function checks for a match at a given location.  It returns
  908.  * 1 if it matches, 0 if it doesn't match here but might match later on in the
  909.  * string, or -1 if it could not possibly match
  910.  */
  911. static int reghelp(prog, string, bolflag)
  912.     struct regexp    *prog;
  913.     char        *string;
  914.     int        bolflag;
  915. {
  916.     char        *scan;
  917.     char        *str;
  918.  
  919.     /* if ^, then require bolflag */
  920.     if ((prog->bol & 1) && !bolflag)
  921.     {
  922.         return -1;
  923.     }
  924.  
  925.     /* if it matches, then it will start here */
  926.     prog->startp[0] = string;
  927.  
  928.     /* compare, possibly ignoring case */
  929.     if (*o_ignorecase)
  930.     {
  931.         for (scan = &prog->program[1]; *scan; scan++, string++)
  932.             if (tolower(*scan) != tolower(*string))
  933.                 return *string ? 0 : -1;
  934.     }
  935.     else
  936.     {
  937.         for (scan = &prog->program[1]; *scan; scan++, string++)
  938.             if (*scan != *string)
  939.                 return *string ? 0 : -1;
  940.     }
  941.  
  942.     /* if $, then require string to end here, too */
  943.     if ((prog->bol & 2) && *string)
  944.     {
  945.         return 0;
  946.     }
  947.  
  948.     /* if we get to here, it matches */
  949.     prog->endp[0] = string;
  950.     return 1;
  951. }
  952.  
  953.  
  954.  
  955. int regexec(prog, string, bolflag)
  956.     struct regexp    *prog;
  957.     char        *string;
  958.     int        bolflag;
  959. {
  960.     int        rc;
  961.  
  962.     /* keep trying to match it */
  963.     for (rc = reghelp(prog, string, bolflag); rc == 0; rc = reghelp(prog, string, 0))
  964.     {
  965.         string++;
  966.     }
  967.  
  968.     /* did we match? */
  969.     return rc == 1;
  970. }
  971. #endif
  972.